package com.yang.controller;

import com.github.pagehelper.PageInfo;
import com.yang.common.PageQuery;
import com.yang.common.ResponseResult;
import com.yang.entity.dto.UserDTO;
import com.yang.entity.vo.UserVO;
import com.yang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户信息接口", value = "用户信息接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    @ApiOperation(value = "新增用户信息")
    public ResponseResult<Integer> add(@RequestBody UserDTO userDTO) {
        int result = userService.add(userDTO);
        if (result == 1) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("新增用户失败");
    }

    @PostMapping("/pageQuery")
    @ApiOperation(value = "分页查询用户信息")
    public ResponseResult<PageInfo<UserVO>> pageQuery(@RequestBody PageQuery<UserDTO> pageQuery) {
        PageInfo<UserVO> result = userService.pageQuery(pageQuery);
        return ResponseResult.success(result);
    }

    @GetMapping("/getUserById")
    @ApiOperation(value = "根据id查询用户信息")
    public ResponseResult<UserVO> getUserById(String id) {
        UserVO result = userService.getUserById(id);
        return ResponseResult.success(result);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息")
    public ResponseResult<Integer> update(@RequestBody UserDTO userDTO) {
        int result = userService.update(userDTO);
        if (result == 1) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("修改用户信息失败");
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除用户信息")
    public ResponseResult<Integer> remove(@RequestBody UserDTO userDTO) {
        int result = userService.remove(userDTO);
        if (result != 0) {
            return ResponseResult.success(result);
        }
        return ResponseResult.fail("删除用户失败");
    }

}
